package jp.co.apl.haven_enquete.android.dao;

import static jp.co.apl.haven.util.HavenProtosUtil.*;

import java.io.IOException;
import java.util.Arrays;
import java.util.Map;

import jp.co.apl.haven.generated.HavenProtos.Category;
import jp.co.apl.haven.generated.HavenProtos.Enquete;
import jp.co.apl.haven.generated.HavenProtos.Question;
import android.test.AndroidTestCase;
import android.test.suitebuilder.annotation.SmallTest;

public class QuestionDataDaoTest extends AndroidTestCase {

    private QuestionDataDao target;

    @Override
    protected void setUp() throws Exception {
        super.setUp();
        target = new QuestionDataDao(mContext);
    }

    @SmallTest
    public void testInitDataFile() throws IOException {
        if (Arrays.asList(mContext.fileList()).contains(
            QuestionDataDao.FILE_NAME)) {
            mContext.deleteFile(QuestionDataDao.FILE_NAME);
        }

        assertFalse(target.isExistsDataFile());

        target.initDataFile();

        assertTrue(target.isExistsDataFile());
    }

    @SmallTest
    public void testLoad() throws IOException {

        target.initDataFile();

        Enquete data = target.load();
        assertEquals(1, data.getVersion());
        assertEquals(6, data.getCategory(0).getQuestionCount());
        assertEquals("施設の種類", data.getCategory(0).getQuestion(0).getTitle());
    }

    @SmallTest
    public void testLoadQuestionDataMap() throws IOException {

        target.initDataFile();

        Map<Integer, Question> map = target.loadAsMap();

        assertFalse(map.isEmpty());
        assertEquals("パソコン", map.get(6).getTitle());
        assertEquals("ライフライン（トイレ）", map.get(5001).getTitle());
    }

    @SmallTest
    public void testNext() {
        Category c1 =
            Category
                .newBuilder()
                .setId(1)
                .setTitle("カテゴリ1")
                .addAllQuestion(
                    qlist(
                        q(
                            1,
                            "質問1",
                            "",
                            sel(
                                "選択肢1-1",
                                q(2, "質問2", "", ""),
                                q(3, "質問3", "", "")),
                            sel(
                                "選択肢1-2",
                                q(4, "質問4", "", ""),
                                q(5, "質問5", "", ""))),
                        q(
                            6,
                            "質問6",
                            "",
                            sel(
                                "選択肢6-1",
                                q(7, "質問7", "", ""),
                                q(8, "質問8", "", "")),
                            sel(
                                "選択肢6-2",
                                q(9, "質問9", "", ""),
                                q(10, "質問10", "", "")))))
                .build();
        Category c2 =
            Category
                .newBuilder()
                .setId(2)
                .setTitle("カテゴリ2")
                .addAllQuestion(
                    qlist(q(
                        11,
                        "質問11",
                        "",
                        sel(
                            "選択肢11-1",
                            q(12, "質問12", "", ""),
                            q(13, "質問13", "", "")),
                        sel(
                            "選択肢11-2",
                            q(14, "質問14", "", ""),
                            q(15, "質問15", "", "")))))
                .build();

        final Enquete enquete =
            Enquete
                .newBuilder()
                .setVersion(1)
                .addCategory(c1)
                .addCategory(c2)
                .build();
        target = new QuestionDataDao(mContext) {
            public Enquete load() {
                return enquete;
            };
        };

        assertEquals(6, target.getNextQuestionId(1));
        assertEquals(3, target.getNextQuestionId(2));
        assertEquals(6, target.getNextQuestionId(3));
        assertEquals(5, target.getNextQuestionId(4));
        assertEquals(6, target.getNextQuestionId(5));
        assertEquals(11, target.getNextQuestionId(6));
        assertEquals(8, target.getNextQuestionId(7));
        assertEquals(11, target.getNextQuestionId(8));
        assertEquals(10, target.getNextQuestionId(9));
        assertEquals(11, target.getNextQuestionId(10));
        assertEquals(-1, target.getNextQuestionId(11));
        assertEquals(13, target.getNextQuestionId(12));
        assertEquals(-1, target.getNextQuestionId(13));
        assertEquals(15, target.getNextQuestionId(14));
        assertEquals(-1, target.getNextQuestionId(15));

    }

    @SmallTest
    public void testPrev() {
        Category c1 =
            Category
                .newBuilder()
                .setId(1)
                .setTitle("カテゴリ1")
                .addAllQuestion(
                    qlist(
                        q(
                            1,
                            "質問1",
                            "",
                            sel(
                                "選択肢1-1",
                                q(2, "質問2", "", ""),
                                q(3, "質問3", "", "")),
                            sel(
                                "選択肢1-2",
                                q(4, "質問4", "", ""),
                                q(5, "質問5", "", ""))),
                        q(
                            6,
                            "質問6",
                            "",
                            sel(
                                "選択肢6-1",
                                q(7, "質問7", "", ""),
                                q(8, "質問8", "", "")),
                            sel(
                                "選択肢6-2",
                                q(9, "質問9", "", ""),
                                q(10, "質問10", "", "")))))
                .build();
        Category c2 =
            Category
                .newBuilder()
                .setId(2)
                .setTitle("カテゴリ2")
                .addAllQuestion(
                    qlist(q(
                        11,
                        "質問11",
                        "",
                        sel(
                            "選択肢11-1",
                            q(12, "質問12", "", ""),
                            q(13, "質問13", "", "")),
                        sel(
                            "選択肢11-2",
                            q(14, "質問14", "", ""),
                            q(15, "質問15", "", "")))))
                .build();

        final Enquete enquete =
            Enquete
                .newBuilder()
                .setVersion(1)
                .addCategory(c1)
                .addCategory(c2)
                .build();
        target = new QuestionDataDao(mContext) {
            public Enquete load() {
                return enquete;
            };
        };

        assertEquals(-1, target.getPrevQuestionId(1));
        assertEquals(1, target.getPrevQuestionId(2));
        assertEquals(2, target.getPrevQuestionId(3));
        assertEquals(1, target.getPrevQuestionId(4));
        assertEquals(4, target.getPrevQuestionId(5));
        assertEquals(1, target.getPrevQuestionId(6));
        assertEquals(6, target.getPrevQuestionId(7));
        assertEquals(7, target.getPrevQuestionId(8));
        assertEquals(6, target.getPrevQuestionId(9));
        assertEquals(9, target.getPrevQuestionId(10));
        assertEquals(6, target.getPrevQuestionId(11));
        assertEquals(11, target.getPrevQuestionId(12));
        assertEquals(12, target.getPrevQuestionId(13));
        assertEquals(11, target.getPrevQuestionId(14));
        assertEquals(14, target.getPrevQuestionId(15));

    }

}
